<?php
require_once("config.php");
require_once("inc.php");
header("Content-type: text/html; charset=utf-8");

$frob = trim($_GET['sid']);
$username = trim($_GET['user']);
if(empty($frob) || empty($username)) {
    die('ERROR:illegal parameter');
}

$params = array(
    'api_key'   =>  KEY,
    'frob'      =>  $frob,
    'method'    =>  'flickr.auth.getToken',
    'format'    =>  'json',
);
$api_sig = _gen_api_sig(SECRET, $params);
$params = array_merge($params, array('api_sig'=>$api_sig));
$res = call('flickr.auth.getToken', $params);
if ($res->stat == 'ok') {
    $token = $res->auth->token->_content;
}

$params = array(
    'api_key'   =>  KEY,
    'username'  =>  $username,
    'format'    =>  'json',
);  
$res = call("flickr.people.findByUsername", $params);
if ($res->stat != 'ok') {
    die("get user nsid failed :(");
}
$nsid = $res->user->nsid;

$p = 1;
$pagesize = 500;
$url_list = array();
$i = 0;

echo '<html xmlns="http://www.w3.org/1999/xhtml"><form action="" method="get"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>原始照片url</title>';
echo '<style type="text/css">tr {border-bottom: 2px solid green;}</style>';
echo '</head><body>';
echo '<table style="border-collapse:collapse; width:680px; text-align:right;">';

do {
    $params = array(
        'auth_token'=>  $token,
        'api_key'   =>  KEY,
        'user_id'   =>  $nsid,
        'per_page'  =>  $pagesize,
        'page'      =>  $p,
        'format'    =>  'json',
    );
    $api_sig = _gen_api_sig(SECRET, array_merge($params, array('method'=>'flickr.people.getPhotos')));
    $params = array_merge($params, array('api_sig'=>$api_sig));

    $res = call("flickr.people.getPhotos", $params);
    if (!is_array($res->photos->photo)) {
        die('<script type="text/javascript">location.href="/flickrget/auth.php";</script>');
    }
    foreach ($res->photos->photo as $obj) {
        $_params = array(
            'api_key'   =>  KEY,
            'photo_id'  =>  $obj->id,
            'format'    =>  'json',
            'secret'    =>  $obj->secret,
        );
        $_res = call("flickr.photos.getInfo", $_params);
        if ($_res->stat == 'ok') {

            $url = "http://farm".$obj->farm.".staticflickr.com/".$obj->server."/".$obj->id."_".$_res->photo->originalsecret."_o.".$_res->photo->originalformat;
            echo '<tr><td style="text-align:right;width:30px;">'.++$i.'</td><td><a href="'.$url.'" target="_blank">'.$url.'</a></td></tr>';
            error_log($url . "\n", 3, ROOT . "/data/source.log." . date("Ym"));
        } else {
            echo '<tr><td style="text-align:right;width:30px;">'.++$i.'</td><td>获取源图信息失败</td></tr>';
            error_log("[time]" . date("Y-m-d H:i:s")  . " [#]" . $i . " [user]" . $username . " [id]" . $obj->id . " [errcode]" . $_res->code . " [errmsg]" . $_res->message . "\n", 3, LOGPATH . '/err.log.' . date("Ymd"));
        }
    }
    if (!isset($total)) {$total = $res->photos->total;}
    if (!isset($pages)) {$pages = $res->photos->pages;}
    $p ++;
} while ($p <= $pages);

echo '</table>';
echo '</body>
    <script  type="text/javascript">
parent.clearTip();
</script>

    </html>';
?>
